

global filename jmp_RTP_targeting_scenarios_new 



foreach proportion_peak of numlist 0 .25 .5 .75 1 {


clear all
use "stata data/in progress 9485", clear 
keep if _n == 1

keep mw_reductions_fe A epsilon avg_kwh customers opt_out_ratio
rename mw_reductions_fe mw_reductions
replace mw_reductions = mw_reductions * -1 

foreach price of numlist 60 110 160 210 85{


capture gen proportion_peak = `proportion_peak' //to tag it for merging





capture gen demand_slope = .6/mw_reductions



//simply make the prices - These prices are above the .25 cent starting b/c they are used to calculate CS
gen pdp_price_`price' = `price'/100

//here i derive the demand reductions using the slope from above
gen mw_reductions_`price' =  pdp_price_`price' / demand_slope

gen mw_reductions_`price'_ces =  (avg_kwh - A*(.24756+pdp_price_`price')^epsilon_ces) * customers*(1-opt_out_ratio) /1000




gen four_hour_dwl_`price' = mw_reductions_`price'*1000 * pdp_price_`price'*.5*4

//do with CES
gen Q_sim_`price' = A*(.24756+pdp_price_`price')^epsilon_ces

gen welfare_tri_per_person_ces_`price' = (A/(epsilon_ces +1))*((.24756+pdp_price_`price')^(epsilon_ces+1)-(.24756)^(epsilon_ces+1))-(pdp_price_`price')*(Q_sim_`price')
gen welfare_loss_triangle_ces_`price' = welfare_tri_per_person_ces_`price' * customers*(1-opt_out_ratio) * 4

gen four_hour_dwl_`price'_ces = welfare_loss_triangle_ces_`price'


gen non_super_peak_15_ed_dwl_`price' = four_hour_dwl_`price' * 12 //this is multiplied by 12 since it's 15-3 aka total minus 3 super peak 
gen non_super_peak_dwl_`price' = four_hour_dwl_`price' * 5

//ces version
gen non_super_peak_15_ed_dwl_`price'_ces = four_hour_dwl_`price'_ces * 12
gen non_super_peak_dwl_`price'_ces = four_hour_dwl_`price'_ces * 5


//super_peak DWL are the DWLs that come from RTP
if `proportion_peak' == 0{
gen super_peak_dwl_`price' = four_hour_dwl_`price' * 3 * (1-1/48)
gen super_peak_dwl_`price'_ces = four_hour_dwl_`price'_ces * 3 * (1-1/48)
}
else {
gen super_peak_dwl_`price' = four_hour_dwl_`price' * 3 * (1-`proportion_peak')
gen super_peak_dwl_`price'_ces = four_hour_dwl_`price'_ces * 3 * (1-`proportion_peak')
}
*

//now adding some co2 value
//roughly this is mw reductions * hours (depends on scenario) * emissions factor of peaker plant per mwh * conversion of pounds to short tons * social cost of carbon (50)

gen co2_value_15_ed_`price' = mw_reductions_`price' * (60-3) * 1239.3 * 0.000453592 * 50 
gen co2_value_`price' = mw_reductions_`price' * (32-3) * 1239.3 * 0.000453592 * 50 


gen co2_value_15_ed_`price'_ces =  mw_reductions_`price'_ces * (60-3) * 1239.3 * 0.000453592 * 50 
gen co2_value_`price'_ces =  mw_reductions_`price'_ces * (32-3) * 1239.3 * 0.000453592 * 50 




gen total_dwl_`price' = non_super_peak_dwl_`price' + super_peak_dwl_`price' - co2_value_15_ed_`price'
gen total_dwl_15_ed_`price' = non_super_peak_15_ed_dwl_`price' + super_peak_dwl_`price' - co2_value_`price'
gen total_dwl_`price'_ces = non_super_peak_dwl_`price'_ces + super_peak_dwl_`price'_ces - co2_value_15_ed_`price'_ces
gen total_dwl_15_ed_`price'_ces = non_super_peak_15_ed_dwl_`price'_ces + super_peak_dwl_`price'_ces - co2_value_`price'_ces



capture gen per_mw_construction_cost = 1159000*184/180 //from page 137 of cec report the 184/180 adjusts to 2016 data using PCCI from 
//https://www.ihs.com/info/cera/ihsindexes/ 
//I compare Q1 2016 to Q1 2011 where the data is indexed to

gen total_construction_costs_`price' = mw_reductions_`price'* per_mw_construction

gen total_construction_costs_`price'_ces = mw_reductions_`price'_ces* per_mw_construction

//it caculates the amortized real payment for the peaker plant today.
//This is the TODAY value I want to use to compare the welfare benefits to the consumer surplus losses
local total_construction_cost_`price' = total_construction_costs_`price'
payper,pv(`total_construction_cost_`price'') nper(30) freq(y) rate(.03)
gen construction_30_year_`price' = `r(PMT)'


local total_construction_cost_`price'_ces = total_construction_costs_`price'_ces
payper,pv(`total_construction_cost_`price'_ces') nper(30) freq(y) rate(.03)
gen construction_30_year_`price'_ces = `r(PMT)'




gen yearly_om_costs_`price' = 25.95 * mw_reductions_`price'*1000

gen yearly_om_costs_`price'_ces = 25.95 * mw_reductions_`price'_ces*1000

gen capacity_welfare_bene_`price' = construction_30_year_`price' +  yearly_om_costs_`price'

gen capacity_welfare_bene_`price'_ces = construction_30_year_`price'_ces +  yearly_om_costs_`price'_ces




gen PDP_welfare_bene_`price' = (capacity_welfare_bene_`price' - total_dwl_`price')
gen PDP_welfare_bene_15_ed_`price' = (capacity_welfare_bene_`price' - total_dwl_15_ed_`price')
gen ratio_`price' = 100* PDP_welfare_bene_`price' / capacity_welfare_bene_`price'



gen PDP_welfare_bene_`price'_ces = (capacity_welfare_bene_`price'_ces - total_dwl_`price'_ces)
gen PDP_welfare_bene_15_ed_`price'_ces = (capacity_welfare_bene_`price'_ces - total_dwl_15_ed_`price'_ces)
gen ratio_`price'_ces = 100* PDP_welfare_bene_`price'_ces / capacity_welfare_bene_`price'_ces




}


save "stata data//comparison to RTP peak ratio_`proportion_peak'", replace

}



*
use "stata data//comparison to RTP peak ratio_0", clear
foreach proportion_peak of numlist .25 .5 .75 1 {
append using "stata data//comparison to RTP peak ratio_`proportion_peak'"
}



//here we set the values. TO get the actual prices add 25 cents

local low = 60
local medium 110
local high 160

local base 110


//So I compare everything to the medium RTP level. I do 



//peak price price is too high here. So you get the medium benefits, and the high DWL costs
gen ratio_PDP_`high'_RTP_`medium'_15_ed = 100* (capacity_welfare_bene_`medium' - total_dwl_15_ed_`high') / capacity_welfare_bene_`base'
gen ratio_PDP_`high'_RTP_`medium' = 100* (capacity_welfare_bene_`medium' - total_dwl_`high') / capacity_welfare_bene_`base'


//this is the optimal case
gen ratio_PDP_`medium'_RTP_`medium'_15_ed = 100* (capacity_welfare_bene_`medium' - total_dwl_15_ed_`medium') / capacity_welfare_bene_`base'
gen ratio_PDP_`medium'_RTP_`medium' = 100* (capacity_welfare_bene_`medium' - total_dwl_`medium') / capacity_welfare_bene_`base'


//peak price price is too low here. So you get the low benefits, and the high low costs. It's divided by the medium benefits to show
//how close we got
gen ratio_PDP_`low'_RTP_`medium'_15_ed = 100* (capacity_welfare_bene_`low' - total_dwl_15_ed_`low') / capacity_welfare_bene_`base'
gen ratio_PDP_`low'_RTP_`medium' = 100* (capacity_welfare_bene_`low' - total_dwl_`low') / capacity_welfare_bene_`base'





keep if proportion_peak == .25 //this is to make it so RTP prices are at peak for 1 hour
keep ratio_PDP*

preserve
keep ratio_PDP_`high'_RTP_`base'_15_ed ratio_PDP_`medium'_RTP_`base'_15_ed ratio_PDP_`low'_RTP_`base'_15_ed
gen days = 15
rename ratio_PDP_`high'_RTP_`base'_15_ed ratio_PDP_`high'_RTP_`base'
rename ratio_PDP_`medium'_RTP_`base'_15_ed  ratio_PDP_`medium'_RTP_`base'
rename ratio_PDP_`low'_RTP_`base'_15_ed ratio_PDP_`low'_RTP_`base'
save "stata data/ed_15 scenarios", replace
restore

drop ratio_PDP_`high'_RTP_`base'_15_ed ratio_PDP_`medium'_RTP_`base'_15_ed ratio_PDP_`low'_RTP_`base'_15_ed
gen days = 8
append using "stata data/ed_15 scenarios"



save "stata data/data for input", replace


capture file close table1		
file open table1 using "latex/$filename.tex", write text replace


file write table1 "\label{$filename}" _n
file write table1 "\vspace{-2mm}" _n
file write table1 "\footnotesize" _n
file write table1 "\begin{tabular}{@{\extracolsep{4pt}}lccc@{}ccccc}" _n //this is where you change justification. Options are l c r
*extracolsep from http://tex.stackexchange.com/questions/101000/how-to-shorten-cline-so-that-multiple-groups-in-a-table-become-visible

file write table1 "\hline\hline \\ [-.9em]" _n 

*file write table1 "\multicolumn{1}{c}{$\begin{matrix}\text{Baseline}\\ \text{Territory} \end{matrix}$}& 	\multicolumn{1}{c}{$\begin{matrix}\text{Average}\\ \text{Temperature} \end{matrix}$} &	\multicolumn{1}{c}{$\begin{matrix}\text{Firm}\\ \text{Count} \end{matrix}$}	&	\\" _n
*file write table1 " Energy use classification && Median & & &Thirds	\\" _n 
*file write table1 "  \cline{2-4} \cline{5-7} \\" _n 
*file write table1 "[0.2em]" _n


file write table1 "  & (1) & (2) & (3)  \\" _n
file write table1 "[0.2em]" _n

local col1_top "Event days called per summer" 
local col1_bottom ""

local col2_top "\char36 .85/kWh peak price"
local col2_bottom "(peak price \$\textless\$ RTP)"
local col3_top "\char36 1.35/kWh peak price"
local col3_bottom "(peak price = RTP)"
local col4_top "\char36 1.85/kWh peak price"
local col4_bottom "(peak price \$\textgreater\$ RTP)"
local col5_top "\char36 1.80 PDP"
local col5_bottom "price"



local col1 "\multicolumn{1}{l}{$\begin{matrix}\text{`col1_top'}\\ \text{`col1_bottom'} \end{matrix}$}"
local col2 "\multicolumn{1}{c}{$\begin{matrix}\text{`col2_top'}\\ \text{`col2_bottom'} \end{matrix}$}"
local col3 "\multicolumn{1}{c}{$\begin{matrix}\text{`col3_top'}\\ \text{`col3_bottom'} \end{matrix}$}"
local col4 "\multicolumn{1}{c}{$\begin{matrix}\text{`col4_top'}\\ \text{`col4_bottom'} \end{matrix}$}"
local col5 "\multicolumn{1}{c}{$\begin{matrix}\text{`col5_top'}\\ \text{`col5_bottom'} \end{matrix}$}"




file write table1 " `col1_top' & `col2' & `col3' & `col4' 	\\" _n



file write table1 "[0.1em]" _n
file write table1 "\hline \\" _n
file write table1 "[-0.7em]" _n




capture gen row = _n


gen event_days_label = ""

replace event_days_label = "8 event days (well targeted)" if days == 8
replace event_days_label = "15 event days (current)" if days == 15




gen id = _n

foreach var of varlist ratio_PDP_*{
replace `var'= round(`var',1)
tostring `var', replace force
replace `var' = `var' + "\%"
}



replace ratio_PDP_110_RTP_110 = "\textbf{" + ratio_PDP_110_RTP_110 +"}" if row == 1
replace ratio_PDP_60_RTP_110 = "\textbf{" + ratio_PDP_60_RTP_110 +"}" if row == 2




levelsof id, local(vars)
foreach v of local vars  {
  qui sum row if id==`v'
  local r = r(mean)
  
  	
	local r = r(mean)
	local t1 = event_days_label[`r']
	local t2 = ratio_PDP_`low'_RTP_`base'[`r'] 
	local t3 =  ratio_PDP_`medium'_RTP_`base'[`r'] 
	local t4 = ratio_PDP_`high'_RTP_`base'[`r'] 
	*local t5 = ratio_180[`r']
	
	

	file write table1 "`t1' & `t2' & `t3' & `t4'    \\" _n
	file write table1 "[0.2em] " _n
	}

*


	


file write table1 "\hline" _n
file write table1 "\end{tabular}" _n

file close table1		
